<template>
  <div class="app-container">
    <SearchForm
      v-show="showSearch"
      v-model="queryParams"
      :options="options.formOps"
      @onQuery="getList"
    >
    </SearchForm>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['key:personnel:admin:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <ImportExcel
          type="primary"
          full-path="/key/personnel/admin/importData"
          plain
          icon="Plus"
          @success="getList"
          v-hasPermi="['key:personnel:admin:importData']"
        ></ImportExcel>
      </el-col>
      <el-col :span="1.5">
        <ExportExcel
          type="primary"
          full-path="/key/personnel/admin/importTemplate"
          v-hasPermi="['key:personnel:admin:importTemplate']"
          plain
        ></ExportExcel>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport"
          >导出</el-button
        >
      </el-col>
      <!-- <el-col :span="3" v-if="count.personnelCount"
        >人员总数: {{ count.personnelCount }}
      </el-col>
      <el-col :span="3" v-if="count.soleCount"
        >专职人员总数: {{ count.soleCount }}
      </el-col>
      <el-col :span="3" v-if="count.logonCount"
        >注册安全工程师人数: {{ count.logonCount }}
      </el-col>
      <el-col :span="4" v-if="count.tagOne"
        >安全工程师不满足标签: {{ count.tagOne }}
      </el-col>
      <el-col :span="4" v-if="count.tagTwo"
        >安全管理人员不满足标签: {{ count.tagTwo }}
      </el-col> -->

      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <Table
      ref="table"
      :auto-featch="true"
      :fetch-data="fetchData"
      :options="options.table"
      actionWidth="230"
    >
<!--      <template #beforepost>
		<el-table-column
		  label="人员编码"
		  align="center"
		  key="startTime"
		  prop="staffCode"
		>
        </el-table-column>
      </template> -->
      <template #actionButton="{ row }">
        <el-button type="text" @click="handleDetail(row)">证书信息</el-button>
        <el-button
          type="text"
          icon="Edit"
          @click="handleEdit(row)"
          v-hasPermi="['key:personnel:admin:edit']"
          >修改</el-button
        >
        <el-button
          type="text"
          icon="Delete"
          style="color: red"
          @click="handleDel(row)"
          v-hasPermi="['key:personnel:admin:delete']"
          >删除</el-button
        >
      </template></Table
    >
    <DialogVform
      :formJson="formJson"
      ref="dialogVform"
      @onSubmit="onSubmit"
      :slots="['departmentName']"
      :title="dialogType == 'add' ? '新增两类重点人员' : '修改两类重点人员'"
    >
      <template #departmentName>
        <el-form-item label="所属部门" prop="planDeptId" label-position="right">
          <TreeSelect
            v-model="form.departmentId"
            :options="deptTree"
            @change="changeDept"
            style="width: 100%"
          >
          </TreeSelect>
        </el-form-item>
      </template>
      <template #fileSlot>
        <el-form-item label="安全证书">
          <FileUpload
            :limit="1"
            :fileSize="30"
            :fileType="['doc', 'docx', 'pdf']"
            v-model="files"
          ></FileUpload>
        </el-form-item>
      </template>
    </DialogVform>
    <el-dialog v-model="outerVisible" width="90%" title="证书信息">
      <Inner ref="inner" :parentNode="parentNode"></Inner>
    </el-dialog>
  </div>
</template>

<script setup>
import Table from "@/components/Table/index.vue";
import SearchForm from "@/components/searchForm/index";
import DialogVform from "@/components/dialogVform/index";
import { apis as APIS } from "@/api/aqsc/xgfgl/llzdry.js";
import formJson from "./components/options.json";
import { useAsyncData, useFormJsonNew } from "@/views/hooks/useAsyncData.js";
import { deptTreeSelect } from "@/api/system/user";
import Inner from "./components//inner.vue";
import ImportExcel from "@/components/ImportExcel/index.vue";
import ExportExcel from "@/components/ExportExcel/index.vue";
const { proxy } = getCurrentInstance();

const ops = useFormJsonNew(formJson, {
  sort: [],
  sortable: [],
});
const options = reactive({
  table: ops,
  formOps: ops.filter((i) =>
    ["name", "record", "phone", "visitDepartment", "departmentName"].includes(
      i.prop
    )
  ),
});
const files = ref([]);
watch(
  () => files,
  (newData) => {
    if (newData.value.length === 0) {
      form.value.securityFileName = "";
      form.value.securityFileId = "";
      form.value.securityFileUrl = "";
    } else {
      form.value.securityFileName = newData.value[0].name;
      form.value.securityFileId = newData.value[0].id;
      form.value.securityFileUrl = newData.value[0].url;
    }
  },
  {
    deep: true,
  }
);
const plugAdd = () => {
  files.value = [];
};
const plugEdit = (row) => {
  form.value.departmentName = row.departmentName;
  form.value.departmentId = row.departmentId;
  if (row.securityFileId) {
    files.value = [
      {
        name: row.securityFileName,
        id: row.securityFileId,
        url: row.securityFileUrl,
      },
    ];
  } else {
    files.value = [];
  }
};

const showSearch = ref(true);

const dialogVform = ref();

const table = ref();

const {
  onSubmit,
  fetchData,
  handleAdd,
  handleEdit,
  getList,
  handleDel,
  queryParams,
  dialogType,
  form,
  handleExport,
} = useAsyncData({
  APIS: {
    ...APIS,
    url: "/key/personnel/admin/",
  },
  formJson,
  dialogVform,
  table,
  proxy,
  plugEdit,
  plugAdd,
});

const deptTree = ref([]);
const count = ref({});
onMounted(() => {
  deptTreeSelect().then((response) => {
    deptTree.value = response.data;
  });
  APIS.count().then((res) => {
    count.value = res;
  });
});
const changeDept = (val, label) => {
  form.value.departmentName = label;
  form.value.departmentId = val;
};
// 类型
const inner = ref();
const parentNode = ref({});
const outerVisible = ref(false);
const handleDetail = (row) => {
  outerVisible.value = true;
  nextTick().then(() => {
    parentNode.value = row;
  });
};
</script>

<style lang="scss" scoped></style>
